package com.ruoyi.web.sso.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.common.util.UuidUtil;
import com.ruoyi.web.sso.domain.PushAddress;
import com.ruoyi.web.sso.domain.PushDeviceInfo;
import com.ruoyi.web.sso.domain.vo.AllPushNews;
import com.ruoyi.web.sso.domain.vo.PushNews;
import com.ruoyi.web.sso.mapper.PushAddressMapper;
import com.ruoyi.web.sso.mapper.PushDeviceInfoMapper;
import com.ruoyi.web.sso.util.AppPushUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.sso.mapper.PushRecordMapper;
import com.ruoyi.web.sso.domain.PushRecord;
import com.ruoyi.web.sso.service.IPushRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 设备信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-23
 */
@Service
public class PushRecordServiceImpl implements IPushRecordService {
    @Autowired
    private PushRecordMapper pushRecordMapper;

    @Autowired
    private PushAddressMapper pushAddressMapper;

    @Autowired
    private PushDeviceInfoMapper pushDeviceInfoMapper;

    /**
     * 查询设备信息
     *
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public PushRecord selectPushRecordById(String id) {
        return pushRecordMapper.selectPushRecordById(id);
    }

    /**
     * 查询设备信息列表
     *
     * @param pushRecord 设备信息
     * @return 设备信息
     */
    @Override
    public List<PushRecord> selectPushRecordList(PushRecord pushRecord) {
        return pushRecordMapper.selectPushRecordList(pushRecord);
    }

    /**
     * 新增设备信息
     *
     * @param pushRecord 设备信息
     * @return 结果
     */
    @Override
    public int insertPushRecord(PushRecord pushRecord) {
        pushRecord.setCreateTime(DateUtils.getNowDate());
        return pushRecordMapper.insertPushRecord(pushRecord);
    }

    /**
     * 修改设备信息
     *
     * @param pushRecord 设备信息
     * @return 结果
     */
    @Override
    public int updatePushRecord(PushRecord pushRecord) {
        pushRecord.setUpdateTime(DateUtils.getNowDate());
        return pushRecordMapper.updatePushRecord(pushRecord);
    }

    /**
     * 批量删除设备信息
     *
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deletePushRecordByIds(String ids) {
        return pushRecordMapper.deletePushRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备信息信息
     *
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deletePushRecordById(String id) {
        return pushRecordMapper.deletePushRecordById(id);
    }

    @Override
    public void addPush(PushNews pushNews) {
        PushDeviceInfo pushDeviceInfo = new PushDeviceInfo();
        pushDeviceInfo.setClientId(pushNews.getClientId());
        List<PushDeviceInfo> pushDeviceInfoList = pushDeviceInfoMapper.selectPushDeviceInfoList(pushDeviceInfo);
        if (AssertUtil.isEmpty(pushDeviceInfoList)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "不能为空");
        }

        PushAddress pushAddress = new PushAddress();
        pushAddress.setCode(pushNews.getAppCode());
        List<PushAddress> pushAddressList = pushAddressMapper.selectPushAddressList(pushAddress);
        if (AssertUtil.isEmpty(pushAddressList)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "不能为空");
        }
        //发送消息
        AppPushUtil.addPush(pushAddressList.get(0).getAppId(), pushAddressList.get(0).getAppKey(), pushAddressList.get(0).getMasterSecret(), pushAddressList.get(0).getUrl() ,
                pushNews.getType(), pushNews.getTitle(), pushNews.getContent(), pushNews.getClientId());

        PushRecord pushRecord = new PushRecord();
        pushRecord.setId(UuidUtil.getUuid());
        pushRecord.setClientId(pushNews.getClientId());
        pushRecord.setCode(pushNews.getAppCode());
        pushRecord.setContent( pushNews.getContent());
        pushRecord.setMobileType(pushNews.getMobileType());
        pushRecord.setTitle(pushNews.getTitle());
        pushRecord.setRemark(pushAddressList.get(0).getId());
        pushRecordMapper.insertPushRecord(pushRecord);
    }


    @Override
    public void allPush(AllPushNews allPushNews) {

        PushAddress pushAddress = new PushAddress();
        pushAddress.setCode(allPushNews.getAppCode());
        List<PushAddress> pushAddressList = pushAddressMapper.selectPushAddressList(pushAddress);
        if (AssertUtil.isEmpty(pushAddressList)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "不能为空");
        }

        PushDeviceInfo pushDeviceInfo = new PushDeviceInfo();
        pushDeviceInfo.setAppCode(allPushNews.getAppCode());
        List<PushDeviceInfo> pushDeviceInfoList = pushDeviceInfoMapper.selectPushDeviceInfoList(pushDeviceInfo);
        if (AssertUtil.isEmpty(pushDeviceInfoList)) {
            throw new BusinessException(ErrorCode.UNDEFINED, "不能为空");
        }
        for (int i = 0; i < pushDeviceInfoList.size(); i++) {
            //发送消息
            AppPushUtil.addPush(pushAddressList.get(0).getAppId(), pushAddressList.get(0).getAppKey(), pushAddressList.get(0).getMasterSecret(), pushAddressList.get(0).getUrl() ,
                    Integer.valueOf(pushDeviceInfoList.get(i).getType()), allPushNews.getTitle(), allPushNews.getContent(),  pushDeviceInfoList.get(i).getClientId());
            PushRecord pushRecord = new PushRecord();
            pushRecord.setId(UuidUtil.getUuid());
            pushRecord.setClientId(pushDeviceInfoList.get(i).getClientId());
            pushRecord.setCode(allPushNews.getAppCode());
            pushRecord.setContent( allPushNews.getContent());
            pushRecord.setMobileType(pushDeviceInfoList.get(i).getName());
            pushRecord.setTitle(allPushNews.getTitle());
            pushRecord.setRemark(pushAddressList.get(0).getId());
            pushRecordMapper.insertPushRecord(pushRecord);
        }

    }
}
